// *******************************************************************************
// **         Autor: RAVEN
// **          Site: www.ArmedAssault.com.ar
// **         Forum: www.ArmedAssault.com.ar/Foros
// *******************************************************************************

// Taken selections of original code from Raven for RUM Assets by Draper

_veh = _this select 0;
_obj = _this select 1;

_class = "Eslinga_L";
_class2 = "Eslinga_M";
_genclass = "Eslinga";

if (isClass(configFile >> "CfgPatches" >> "ace_main"))then{
			hint "ACE";
	_class = "ace_slingrope_L";
	_class2 = "ace_slingrope_M";
	_genclass = "ace_slingrope";
};

_center = _veh getvariable "GeoLifter";
if ((typeOf _veh) in ["CYBP_AH6_US","CYBP_AH6_RACS","CYBP_AH6_NAPA","CYBP_AH6_CHDKZ","CYBP_MH6_US","CYBP_MH6_RACS","CYBP_MH6_NAPA","CYBP_MH6_CHDKZ"]) then {_center = [0,-0.8,0.5]};
if ((typeOf _veh) in ["UH1Y"]) then {_center = [0,-6.5,0.4]};
if ((typeOf _veh) in ["UH60M_EP1"]) then {_center = [0,0,0.5]};
if ((typeOf _veh) in ["USMC_MH53E"]) then {_center = [0,0,1]};
if ((typeOf _veh) in ["USMC_MH53E"]) then {_center = [0,0,1]};
if(isNil ("_center"))then{_center = [0,0,0]};

_cx = (_center select 0);
_cy = (_center select 1);
_cz = (_center select 2);

_dtV  = boundingBox _veh;
_lng  = abs((_dtV select 0) select 2) + (abs((_dtV select 1) select 2) * 1.2);
_anch = abs((_dtV select 0) select 0) +  abs((_dtV select 1) select 0);
_alt  = abs((_dtV select 0) select 1) +  abs((_dtV select 1) select 1);
_volVeh = (_lng * _anch) * _alt;

_dtOb = boundingBox _obj;
_anch = abs((_dtOb select 0) select 0) + abs((_dtOb select 1) select 0);
_lng  = abs((_dtOb select 0) select 1) + abs((_dtOb select 1) select 1);
_alt  = abs((_dtOb select 0) select 2) + abs((_dtOb select 1) select 2);
_volObj = (_lng * _anch) * _alt;

_center = _obj getvariable "GeoObject";
if (not isnil("_center")) then
{
	_Obx = (_center select 0);
	_Oby = (_center select 1);
	_Obz = (_center select 2);
} else {_Obx = 0; _Oby = 0; _Obz = 0};

_dist = (_veh distance _obj) * (-1);
_obj attachTo [_veh,[0,0,_dist]];
_dd = (direction _obj) - (direction _veh);

_tipEslinga = 3;
if (_volObj > 0.001) then
{
	_rel = (_volVeh / _volObj);
	if (_rel >= _tope) then
	{
		_tipEslinga = 3;
		if (_volObj < 400) then {_tipEslinga = 3};
		if (_volObj < 200) then {_tipEslinga = 2};
		if (_volObj < 150) then {_tipEslinga = 1};
		if (_rel < 20) then
		{
			_tipEslinga = 1;
			_volObj = ((_lng * 100) / _anch);
			if (_volObj > 100) then {_volObj = _volObj - 100};
			if (_volObj > 20) then {_tipEslinga = 3};
		};
	};
};

if ("landvehicle" counttype [_obj] == 0) then {_tipEslinga = 1};
if ("motorcycle" counttype [_obj] > 0) then {_tipEslinga = 1};
if ("StaticWeapon" counttype [_obj] > 0) then {_tipEslinga = 1};
if (_tipEslinga == 3) then {_tipEslinga = 2};
		
_cable = objNull;
			
switch (_tipEslinga) do
{
	case 1:
	{
		_cable = _class createvehicle [0,0,0];
	};
	case 2:
	{
		_cable = _class2 createvehicle [0,0,0];
	};
	default{hint format ["%1",_tipEslinga];}
};

_cable attachto [_veh, [_cx+0,_cy+1,_cz-4.5]];
_obj attachto [_cable, [0,0,0.75], "anclaje"];
_obj setDir 0;
_obj setpos getpos _obj;

_veh setvariable ["lift",true,true];

_veh spawn {
	while {(_this getvariable "lift") and (canmove _this) and (alive _this)} do
	{
		_this limitSpeed 100;
		sleep 0.05;
	};
};

waituntil{!(_veh getvariable "lift") or !(alive _veh)};

detach _cable;
deletevehicle _cable;
_cable = _genclass createvehicle [0,0,0];
_cable attachto [_veh, [_cx+0,_cy+1,_cz-4.5]];
sleep 1.75;
detach _cable;
deletevehicle _cable;

_stg = format ["%1",_veh];
waitUntil{(!alive _veh) or (RUM_SUP_endflight == _stg)};
detach _cable;
deletevehicle _cable;

if(true)exitwith{};